package com.psbc.springbootstudydemo1.controller;

import com.psbc.springbootstudydemo1.asynctask.AsyncTask;
import com.psbc.springbootstudydemo1.config.WXConfig;
import com.psbc.springbootstudydemo1.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
@PropertySource(value = "classpath:pay.properties")
@RequestMapping("testcontroller")
public class TestController {
    @Autowired
    private AsyncTask asyncTask;
//    @Autowired
//    private WXConfig wxConfig;
//    @GetMapping("test")
//    public JsonData test(){
//        HashMap<String, String> map = new HashMap<>();
//        map.put("appid",wxConfig.getAppid());
//        map.put("secret",wxConfig.getSecret());
//        return JsonData.buildSuccess(map);
//    }
    @Value("${wxpay.appid}")
    private String appid;
    @Value("${wxpay.secret}")
    private String secret;

    @GetMapping("test1")
    public JsonData test(){
        HashMap<String, String> map = new HashMap<>();
        map.put("appid",appid);
        map.put("secret",secret);
        return JsonData.buildSuccess(map);
    }
    @GetMapping("test2")
    public JsonData testHandler(){
        int i = 1 / 0;
        return JsonData.buildSuccess("");
    }
    @GetMapping("test3")
    public JsonData asyncTask(){
        long begin = System.currentTimeMillis();
//        asyncTask.task1();
//        asyncTask.task2();
//        asyncTask.task3();
        Future<String> task4 = asyncTask.task4();
        Future<String> task5 = asyncTask.task5();
        for (;;){
            if (task4.isDone() && task5.isDone()){
                try {
                    String task4Res = task4.get();
                    System.out.println(task4Res);
                    String task5Res = task5.get();
                    System.out.println(task5Res);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }finally {
                    break;
                }
            }
        }
        long end = System.currentTimeMillis();
        return JsonData.buildSuccess(end - begin);
    }
}
